package com.butterfly.encrypt.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.ByteBuffer;

import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
 * 工具类
 * @author fox email:hitman__47@163.com
 * @version 1.0 2013-10-29 18:17
 *
 */
public class Tools {
	private static final  Logger log = Logger.getLogger(Tools.class);
	private static final String digits = "0123456789abcdef";
	/**
	 * 将指定的DOC按encoding解包成字节流
	 * @param doc
	 * 				待解包的DOC
	 * @param encoding
	 * 				 字符编码
	 * 			
	 * @return 
	 * 			XML的字节数组形式
	 * @throws IOException 
	 */
	public static byte[] getXmlBytes(Document doc,String encoding) throws IOException{
			Format format = Format.getCompactFormat();
			format.setEncoding(encoding);//设置xml文件的字符为encoding
			format.setIndent("    "); //设置xml文件的缩进为4个空格
			XMLOutputter XMLOut = new XMLOutputter(format);// 在元素后换行，每一层元素缩排四格
			ByteArrayOutputStream data = new ByteArrayOutputStream();
			
			XMLOut.output(doc, data);
			return data.toByteArray();
	}
	/**
	 * 按系统默认的字符集进行编码
	 * @see #getXmlBytes(Document, String)
	 * @param doc
	 * 			  待解包的DOC
	 * @return
	 * 			  XML的 字节数组形式
	 * @throws IOException 
	 */
	public static byte[] getXmlBytes(Document doc) throws IOException{
		 return getXmlBytes(doc,getFileEncoding("UTF-8"));
		
	}
	/**
	 * 获取系统默认文件编码字符集
	 * @param def
	 * @return 系统默认文件编码字符集
	 */
	public static String getFileEncoding(String def) {
		return System.getProperty("file.encoding", def);
	}
	/**
	 * 将指定的DOC按encoding解包成字符串
	 * @param doc
	 * 			  待解包的DOC
	 * @param encoding
	 * 		   	  字符编码
	 * @return
	 * 			Document的String形式
	 * @throws IOException 
	 */
	public static String getXmlString(Document doc,String encoding) throws IOException{
		
		StringWriter memo = new StringWriter();
		Format format =Format.getPrettyFormat();
		format.setEncoding(encoding);
		
		XMLOutputter XMLOut= new XMLOutputter(format);
		XMLOut.output(doc, memo);
		return memo.getBuffer().toString();
	}
	/**
	 * 按系统默认字符集解包成字符串
	 * @param doc
	 * 			  待解包的DOC
	 * @see  #getXmlString(Document, String)
	 * @return
	 * 			Document 的 String 形式
	 * @throws IOException 
	 */
	public static String getXmlString(Document doc) throws IOException{
		return getXmlString(doc, getFileEncoding("UTF-8"));
	}
	/**
	 * 将字节数组转成Document
	 * @param docBytes
	 * @return
	 * @throws IOException 
	 * @throws JDOMException 
	 */
	public static Document getXmlDoc(byte[] docBytes) throws JDOMException, IOException{
		return getXmlDoc(docBytes,0,docBytes.length);
	}
	/**
	 *  将字节数组转成Document
	 * @param docBytes
	 * 					字节流
	 * @param offset
	 * 					偏移量
	 * @param length
	 * 					需要打包的长度
	 * @return
	 * 					Document
	 * @throws IOException 
	 * @throws JDOMException 
	 */
	public static Document getXmlDoc(byte[] docBytes, int offset, int length) throws JDOMException, IOException {
		SAXBuilder saxBuilder = new SAXBuilder();
		return saxBuilder.build(new ByteArrayInputStream(docBytes,offset,length));
	}
	
	/**
	 * Docuemnt 序列化成 byte 流
	 * @param document
	 * 				  document
	 * @return
	 * 				  序列化的字节流数组
	 * @throws IOException 
	 * 			
	 * 			
	 */
	public static byte[] toByteArrays(Document document) throws IOException {
		ByteArrayOutputStream boutput = new ByteArrayOutputStream();
		Format format = Format.getCompactFormat();
		format.setEncoding("UTF-8");
		XMLOutputter xmlOutput = new XMLOutputter(format);
		xmlOutput.output(document, boutput);
		return new String(boutput.toByteArray()).trim().getBytes();
	}
	/**
	 * 将字节数组变为可显示的字符串
	 * @param data
	 * 				二进制数组
	 * @return
	 */
	public static String byte2HexString(byte[] data){
		
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i != data.length; i++) {
			int v = data[i] & 0xff;
			buf.append(digits.charAt(v >> 4));
			buf.append(digits.charAt(v & 0xf));
		}
		return buf.toString().toUpperCase();
	}
	/**
	 * 将十六进制字符串转换成byte数组
	 * @param hex
	 * 				十六进制字符串
	 * @return
	 * 				byte数组
	 */
	public static byte[] hexStr2Byte(String hex){
		ByteBuffer bf = ByteBuffer.allocate(hex.length()/2);
		for(int i=0;i<hex.length();i++){
			String hexStr=hex.charAt(i)+"";
			i++;
			hexStr+=hex.charAt(i);
			byte b = (byte)Integer.parseInt(hexStr,16);
			bf.put(b);
		}
		return bf.array();
	}
	public static void main(String[] args) {
		byte [] b ="00119".getBytes();
		System.out.println(byte2HexString(b));
		System.out.println(hexStr2Byte("61736566657366657366657366656665").toString());
	}
}
